package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.HomeTypeLeftAdapter;
import com.yiwei.zxp.adapter.HomeTypeRightAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ShopMallListBean;
import com.yiwei.zxp.api.model.ShopIndexModel;
import com.yiwei.zxp.api.model.ShopMallListModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class HomeTypeActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.btn_message)
    ImageView btnMessage;
    @BindView(R.id.list_left)
    RecyclerView listLeft;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.list_right)
    RecyclerView listRight;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.txt_message_num)
    TextView txtMessageNum;
    private Unbinder unbiner;
    private String cate_id;
    private HomeTypeLeftAdapter homeTypeLeftAdapter;
    private HomeTypeRightAdapter homeTypeRightAdapter;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            onLoadeMoreListener.setPage(1);
            getShopMallList(page);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_type);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        homeTypeLeftAdapter = new HomeTypeLeftAdapter(this);
        homeTypeLeftAdapter.setOnChooseListener(new HomeTypeLeftAdapter.OnChooseListener() {
            @Override
            public void onClick(String id) {
                cate_id = id;
                onLoadeMoreListener.setPage(1);
                getShopMallList(1);
            }
        });
        listLeft.setAdapter(homeTypeLeftAdapter);
        homeTypeRightAdapter = new HomeTypeRightAdapter(this);
        listRight.addOnScrollListener(onLoadeMoreListener);
        listRight.setAdapter(homeTypeRightAdapter);


        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getIndex();
                getMsgTotal();
            }
        });
        getIndex();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (UserInfoGlobal.isLogin()) {
            getMsgTotal();
        }
    }

    public void getMsgTotal() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        Api.api_service.getMessageCenter().compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.code.equals("00")) {
                        if (result.datas.totalCount == 0) {
                            txtMessageNum.setVisibility(View.GONE);
                        } else {
                            txtMessageNum.setVisibility(View.VISIBLE);
                            txtMessageNum.setText(result.datas.totalCount+"");
                        }
                    }

                });
    }

    public void getIndex() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);

        Api.api_service.getShopIndex().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setIndexData(result.datas);
                    if (homeTypeLeftAdapter.getItemCount() != 0 && TextUtils.isEmpty(cate_id)) {
                        cate_id = result.datas.cats.get(0).id;
                        onLoadeMoreListener.setPage(1);
                        getShopMallList(1);
                    }
                });
    }

    private void setIndexData(ShopIndexModel.DatasBean datas) {
        homeTypeLeftAdapter.update(datas.cats);
    }

    private void setShopMallList(int page, List<ShopMallListModel.DatasBean> datas) {
        homeTypeRightAdapter.add(datas, page == 1);
        if (homeTypeRightAdapter.getItemCount() == 0) {
            layoutEmpty.setVisibility(View.VISIBLE);
        } else {
            layoutEmpty.setVisibility(View.GONE);
        }
    }


    public void getShopMallList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        ShopMallListBean bean = new ShopMallListBean();
        bean.page = page;
        bean.cate_id = cate_id;
        Api.api_service.getShopMallList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setShopMallList(page, result.datas);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.edit_search, R.id.btn_message})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.edit_search:
                Util.startActivity(this, SearchActivity.class);
                break;
            case R.id.btn_message:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(this, LoginNewActivity.class);
                    return;
                }
                Util.startActivity(this, BussinessMsgTotalActivity.class);
                break;
        }
    }
}
